package net.entelijan.cobean.tree;

import java.awt.Component;
import java.util.List;

import javax.swing.tree.DefaultTreeCellRenderer;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.util.CobeanUtil;

public class DefaultTreeLevelDescription<T> implements ITreeLevelDescription {

	private String childrenMethodName;

	private List<ColumnDesc> columnDescs;
	
	private IIconMapper<T> iconMapper;
	
	private ITreeLevelDescription childDescription;

	public DefaultTreeLevelDescription() {
		super();
	}

	@Override
	public ITreeLevelDescription getChildDescription() {
		return this.childDescription;
	}

	@Override
	public <U> List<U> getChildren(U value) {
		return CobeanTreeUtil.getChildren(value, childrenMethodName);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Component getTreeCellRendererComponent(Object model,
			DefaultTreeCellRenderer defaultComp, boolean sel, boolean expanded,
			boolean leaf, int row, boolean hasFocus) {
		String text = CobeanUtil.createText(model, columnDescs);
		defaultComp.setText(text);
		if (iconMapper != null) {
			defaultComp.setIcon(iconMapper.getIcon((T) model));
		}
		return defaultComp;
	}

	public String getChildrenMethodName() {
		return childrenMethodName;
	}

	public void setChildrenMethodName(String childrenMethodName) {
		this.childrenMethodName = childrenMethodName;
	}

	public List<ColumnDesc> getColumnDescs() {
		return columnDescs;
	}

	public void setColumnDescs(List<ColumnDesc> columnDescs) {
		this.columnDescs = columnDescs;
	}

	public IIconMapper<T> getIconMapper() {
		return iconMapper;
	}

	public void setIconMapper(IIconMapper<T> iconMapper) {
		this.iconMapper = iconMapper;
	}

	public void setChildDescription(ITreeLevelDescription childDescription) {
		this.childDescription = childDescription;
	}

}
